<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="任务id" prop="id" v-if="dataForm.id">
        <el-input v-model="dataForm.id" placeholder="任务id" disabled></el-input>
    </el-form-item>
    <el-form-item label="job key 保证任务唯一性" prop="jobKey">
        <el-input v-model="dataForm.jobKey" placeholder="job key 保证任务唯一性"></el-input>
    </el-form-item>
    <el-form-item label="任务名称" prop="jobName">
        <el-input v-model="dataForm.jobName" placeholder="任务名称"></el-input>
    </el-form-item>
    <el-form-item label="简介" prop="description">
        <el-input v-model="dataForm.description" placeholder="简介"></el-input>
    </el-form-item>
    <el-form-item label="关联批处发布任务ID" prop="publishTaskId">
        <el-input v-model="dataForm.publishTaskId" placeholder="关联批处发布任务ID"></el-input>
    </el-form-item>
    <el-form-item label="源数据库类型" prop="readerDbType">
        <el-input v-model="dataForm.readerDbType" placeholder="源数据库类型"></el-input>
    </el-form-item>
    <el-form-item label="原始数据源ID" prop="readerDbId">
        <el-input v-model="dataForm.readerDbId" placeholder="原始数据源ID"></el-input>
    </el-form-item>
    <el-form-item label="源数据库模式" prop="readerSchema">
        <el-input v-model="dataForm.readerSchema" placeholder="源数据库模式"></el-input>
    </el-form-item>
    <el-form-item label="原始数据源名称" prop="readerDbName">
        <el-input v-model="dataForm.readerDbName" placeholder="原始数据源名称"></el-input>
    </el-form-item>
    <el-form-item label="原始数据表名" prop="readerTableName">
        <el-input v-model="dataForm.readerTableName" placeholder="原始数据表名"></el-input>
    </el-form-item>
    <el-form-item label="原始列名 格式：["0:f1:INT","1:f2:VARCHAR","3:m2:DATATIME"]" prop="readerColumns">
        <el-input v-model="dataForm.readerColumns" placeholder="原始列名 格式：["0:f1:INT","1:f2:VARCHAR","3:m2:DATATIME"]"></el-input>
    </el-form-item>
    <el-form-item label="目标数据库类型" prop="writerDbType">
        <el-input v-model="dataForm.writerDbType" placeholder="目标数据库类型"></el-input>
    </el-form-item>
    <el-form-item label="目标数据源ID" prop="writerDbId">
        <el-input v-model="dataForm.writerDbId" placeholder="目标数据源ID"></el-input>
    </el-form-item>
    <el-form-item label="写schema" prop="writerSchema">
        <el-input v-model="dataForm.writerSchema" placeholder="写schema"></el-input>
    </el-form-item>
    <el-form-item label="任务模式，全量/增量" prop="syncType">
        <el-input v-model="dataForm.syncType" placeholder="任务模式，全量/增量"></el-input>
    </el-form-item>
    <el-form-item label="目标数据源名称" prop="writerDbName">
        <el-input v-model="dataForm.writerDbName" placeholder="目标数据源名称"></el-input>
    </el-form-item>
    <el-form-item label="目标数据表名" prop="writerTableName">
        <el-input v-model="dataForm.writerTableName" placeholder="目标数据表名"></el-input>
    </el-form-item>
    <el-form-item label="目标列名 格式：["0:f1:INT","1:f2:VARCHAR","3:d2:DATETIME"]" prop="writerColumns">
        <el-input v-model="dataForm.writerColumns" placeholder="目标列名 格式：["0:f1:INT","1:f2:VARCHAR","3:d2:DATETIME"]"></el-input>
    </el-form-item>
    <el-form-item label="增量字段名" prop="incFieldName">
        <el-input v-model="dataForm.incFieldName" placeholder="增量字段名"></el-input>
    </el-form-item>
    <el-form-item label="增量字段类型" prop="incFieldType">
        <el-input v-model="dataForm.incFieldType" placeholder="增量字段类型"></el-input>
    </el-form-item>
    <el-form-item label="分区字段名" prop="partionFieldName">
        <el-input v-model="dataForm.partionFieldName" placeholder="分区字段名"></el-input>
    </el-form-item>
    <el-form-item label="分区字段格式 默认：yyyyMMddHHmmss" prop="partionFieldFormat">
        <el-input v-model="dataForm.partionFieldFormat" placeholder="分区字段格式 默认：yyyyMMddHHmmss"></el-input>
    </el-form-item>
    <el-form-item label="写入模式 枚举：APPEND/OVERWRITE" prop="wirteMode">
        <el-input v-model="dataForm.wirteMode" placeholder="写入模式 枚举：APPEND/OVERWRITE"></el-input>
    </el-form-item>
    <el-form-item label="速度限制 格式：{channel:5,record:1000,byte:5}" prop="speedLimit">
        <el-input v-model="dataForm.speedLimit" placeholder="速度限制 格式：{channel:5,record:1000,byte:5}"></el-input>
    </el-form-item>
    <el-form-item label="调度页面配置" prop="timerWebConfig">
        <el-input v-model="dataForm.timerWebConfig" placeholder="调度页面配置"></el-input>
    </el-form-item>
    <el-form-item label="cron表达式" prop="timerCron">
        <el-input v-model="dataForm.timerCron" placeholder="cron表达式"></el-input>
    </el-form-item>
    <el-form-item label="参数配置" prop="config">
        <el-input v-model="dataForm.config" placeholder="参数配置"></el-input>
    </el-form-item>
    <el-form-item label="最后一次执行状态" prop="lastExecuteState">
        <el-input v-model="dataForm.lastExecuteState" placeholder="最后一次执行状态"></el-input>
    </el-form-item>
    <el-form-item label="最后一次执行时间" prop="lastExecuteDate">
        <el-input v-model="dataForm.lastExecuteDate" placeholder="最后一次执行时间"></el-input>
    </el-form-item>
    <el-form-item label="下一次执行时间" prop="nextExecuteDate">
        <el-input v-model="dataForm.nextExecuteDate" placeholder="下一次执行时间"></el-input>
    </el-form-item>
    <el-form-item label="任务定时id" prop="scheduleId">
        <el-input v-model="dataForm.scheduleId" placeholder="任务定时id"></el-input>
    </el-form-item>
    <el-form-item label="是否删除" prop="isDeleted">
        <el-input v-model="dataForm.isDeleted" placeholder="是否删除"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createBy" v-if="dataForm.id">
        <el-input v-model="dataForm.createBy" placeholder="创建人" disabled></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime" v-if="dataForm.id">
        <el-input v-model="dataForm.createTime" placeholder="创建时间" disabled></el-input>
    </el-form-item>
    <el-form-item label="更新人" prop="updateBy" v-if="dataForm.id">
        <el-input v-model="dataForm.updateBy" placeholder="更新人" disabled></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime" v-if="dataForm.id">
        <el-input v-model="dataForm.updateTime" placeholder="更新时间" disabled></el-input>
    </el-form-item>
    <el-form-item label="映射类型" prop="mappingType">
        <el-input v-model="dataForm.mappingType" placeholder="映射类型"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="state">
        <el-input v-model="dataForm.state" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="readerDbLayerId" prop="readerDbLayerId">
        <el-input v-model="dataForm.readerDbLayerId" placeholder="readerDbLayerId"></el-input>
    </el-form-item>
    <el-form-item label="writerDbLayerId" prop="writerDbLayerId">
        <el-input v-model="dataForm.writerDbLayerId" placeholder="writerDbLayerId"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/integration/syncJob'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          id: 0,
          jobKey: '',
          jobName: '',
          description: '',
          publishTaskId: '',
          readerDbType: '',
          readerDbId: '',
          readerSchema: '',
          readerDbName: '',
          readerTableName: '',
          readerColumns: '',
          writerDbType: '',
          writerDbId: '',
          writerSchema: '',
          syncType: '',
          writerDbName: '',
          writerTableName: '',
          writerColumns: '',
          incFieldName: '',
          incFieldType: '',
          partionFieldName: '',
          partionFieldFormat: '',
          wirteMode: '',
          speedLimit: '',
          timerWebConfig: '',
          timerCron: '',
          config: '',
          lastExecuteState: '',
          lastExecuteDate: '',
          nextExecuteDate: '',
          scheduleId: '',
          isDeleted: '',
          createBy: '',
          createTime: '',
          updateBy: '',
          updateTime: '',
          mappingType: '',
          state: '',
          readerDbLayerId: '',
          writerDbLayerId: ''
        },
        dataRule: {
          jobKey: [
            { required: true, message: 'job key 保证任务唯一性不能为空', trigger: 'blur' }
          ],
          jobName: [
            { required: true, message: '任务名称不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '简介不能为空', trigger: 'blur' }
          ],
          publishTaskId: [
            { required: true, message: '关联批处发布任务ID不能为空', trigger: 'blur' }
          ],
          readerDbType: [
            { required: true, message: '源数据库类型不能为空', trigger: 'blur' }
          ],
          readerDbId: [
            { required: true, message: '原始数据源ID不能为空', trigger: 'blur' }
          ],
          readerSchema: [
            { required: true, message: '源数据库模式不能为空', trigger: 'blur' }
          ],
          readerDbName: [
            { required: true, message: '原始数据源名称不能为空', trigger: 'blur' }
          ],
          readerTableName: [
            { required: true, message: '原始数据表名不能为空', trigger: 'blur' }
          ],
          readerColumns: [
            { required: true, message: '原始列名 格式：["0:f1:INT","1:f2:VARCHAR","3:m2:DATATIME"]不能为空', trigger: 'blur' }
          ],
          writerDbType: [
            { required: true, message: '目标数据库类型不能为空', trigger: 'blur' }
          ],
          writerDbId: [
            { required: true, message: '目标数据源ID不能为空', trigger: 'blur' }
          ],
          writerSchema: [
            { required: true, message: '写schema不能为空', trigger: 'blur' }
          ],
          syncType: [
            { required: true, message: '任务模式，全量/增量不能为空', trigger: 'blur' }
          ],
          writerDbName: [
            { required: true, message: '目标数据源名称不能为空', trigger: 'blur' }
          ],
          writerTableName: [
            { required: true, message: '目标数据表名不能为空', trigger: 'blur' }
          ],
          writerColumns: [
            { required: true, message: '目标列名 格式：["0:f1:INT","1:f2:VARCHAR","3:d2:DATETIME"]不能为空', trigger: 'blur' }
          ],
          incFieldName: [
            { required: true, message: '增量字段名不能为空', trigger: 'blur' }
          ],
          incFieldType: [
            { required: true, message: '增量字段类型不能为空', trigger: 'blur' }
          ],
          partionFieldName: [
            { required: true, message: '分区字段名不能为空', trigger: 'blur' }
          ],
          partionFieldFormat: [
            { required: true, message: '分区字段格式 默认：yyyyMMddHHmmss不能为空', trigger: 'blur' }
          ],
          wirteMode: [
            { required: true, message: '写入模式 枚举：APPEND/OVERWRITE不能为空', trigger: 'blur' }
          ],
          speedLimit: [
            { required: true, message: '速度限制 格式：{channel:5,record:1000,byte:5}不能为空', trigger: 'blur' }
          ],
          timerWebConfig: [
            { required: true, message: '调度页面配置不能为空', trigger: 'blur' }
          ],
          timerCron: [
            { required: true, message: 'cron表达式不能为空', trigger: 'blur' }
          ],
          config: [
            { required: true, message: '参数配置不能为空', trigger: 'blur' }
          ],
          lastExecuteState: [
            { required: true, message: '最后一次执行状态不能为空', trigger: 'blur' }
          ],
          lastExecuteDate: [
            { required: true, message: '最后一次执行时间不能为空', trigger: 'blur' }
          ],
          nextExecuteDate: [
            { required: true, message: '下一次执行时间不能为空', trigger: 'blur' }
          ],
          scheduleId: [
            { required: true, message: '任务定时id不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '是否删除不能为空', trigger: 'blur' }
          ],
          mappingType: [
            { required: true, message: '映射类型不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          readerDbLayerId: [
            { required: true, message: 'readerDbLayerId不能为空', trigger: 'blur' }
          ],
          writerDbLayerId: [
            { required: true, message: 'writerDbLayerId不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            if (id) {
            getObj(id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      },
      //重置表单
      closeDialog() {
          this.$refs["dataForm"].resetFields()
      }
    }
  }
</script>
